package com.laituo.juyou.admin.api.supplier;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.laituo.juyou.core.exception.AdminServiceException;
import com.laituo.juyou.core.exception.ExceptionDefinition;
import com.laituo.juyou.core.exception.ServiceException;
import com.laituo.juyou.data.component.CacheComponent;
import com.laituo.juyou.data.domain.ActivityCounponDO;
import com.laituo.juyou.data.domain.ActivityDO;
import com.laituo.juyou.data.domain.SupplierDO;
import com.laituo.juyou.data.dto.ActivityDTO;
import com.laituo.juyou.data.dto.EnumsDTO;
import com.laituo.juyou.data.enums.ActivityType;
import com.laituo.juyou.data.mapper.ActivityCouponMapper;
import com.laituo.juyou.data.mapper.ActivityMapper;
import com.laituo.juyou.data.mapper.SupplierMapper;
import com.laituo.juyou.data.model.Page;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * 活动管理服务层
 *
 * @author kaixin
 * @date 2020/03/01
 */
@Service
public class SupplierServiceImpl implements SupplierService {


    @Resource
    private SupplierMapper supplierMapper;


    /**
     * 新增供应商信息
     *
     * @param supplierDO 供应商对象
     * @return ok
     * @throws ServiceException 统一捕获错误
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String create(SupplierDO supplierDO) throws ServiceException {
        supplierDO.setGmtCreate(DateUtil.date());
        supplierDO.setGmtUpdate(DateUtil.date());
        supplierMapper.insert(supplierDO);
        return "ok";
    }


    /**
     * 查询供应商信息 by id
     *
     * @param id 主键id
     * @return 供应商对象
     * @throws ServiceException
     */
    @Override
    public SupplierDO queryActivityById(Long id) throws ServiceException {
        SupplierDO supplierDO = new SupplierDO();
        supplierDO.setId(id);
        return supplierMapper.selectOne(supplierDO);
    }

    /**
     * 列表查询
     * @param page
     * @param limit
     * @param title
     * @return
     * @throws ServiceException
     */
    @Override
    public Page<SupplierDO> list(Integer page, Integer limit, String title) throws ServiceException {
        Wrapper<SupplierDO> wrapper = new EntityWrapper<>();
        List<SupplierDO> supplierDOS = supplierMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        Integer count = supplierMapper.selectCount(wrapper);
        return new Page<>(supplierDOS, page, limit, count);
    }


}
